package com.example.carproviderbackstage.service.impl.dl.admin;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.carinterface.service.dl.admin.OrderService;
import com.example.carinterface.utils.ResultJson;
import com.example.carproviderbackstage.mapper.dl.admin.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {


    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ResultJson<List<Map<String, Object>>> mapListResultJson;
    @Autowired
    private ResultJson<String> stringResultJson;


    @Override
    public ResultJson<List<Map<String, Object>>> findAllOrder(Map<String, Object> map) {

        if (map == null) {
            map = new HashMap<>();
        }


        List<Map<String, Object>> list = orderMapper.findAllOrder(map);

        mapListResultJson.setSuccess("查询成功", list);

        return mapListResultJson;
    }

    @Override
    @Transactional
    public ResultJson<String> updateOrderOfDriver(Map<String, Object> map) {

        List<Map<String, Object>> allOrder = orderMapper.findAllOrder(map);
        if (allOrder != null && allOrder.size() == 1) {
            Map<String, Object> order = allOrder.get(0);
            Integer state = (Integer) order.get("state");
            //司机已结单
            if (state == 2) {
                //将司机添加到司机业务表中
                int i = orderMapper.createDriverBusiness(map);
                Object driverBusiness_id = map.get("driverBusiness_id");
                System.out.println(driverBusiness_id);
                //将司机业务表绑定到订单表中
                int j = orderMapper.updateOrderDriver(map);
                map.put("state", 3);
                int k = orderMapper.addOrderState(map);
                if (i == 1 && j == 1 && k == 1) {
                    stringResultJson.setSuccess("派遣司机成功");
                } else {
                    stringResultJson.setSuccess("派遣司机失败");
                }
                //派遣了司机，但还未接单
            } else if (state == 3) {
                int j = orderMapper.updateDriverBusinessOfDriver(map);
                if (j == 1) {
                    stringResultJson.setSuccess("修改司机成功");
                } else {
                    stringResultJson.setSuccess("修改司机失败");
                }
            } else {
                stringResultJson.setSuccess("修改失败，你不配");
            }
            //订单类型不匹配
        } else {
            stringResultJson.setSuccess("修改失败,参数错误");
        }
        return stringResultJson;
    }

    @Override
    @Transactional
    public ResultJson<String> addOrder(Map<String, Object> map) {

        Map<String, Object> map1 = orderMapper.findOwnerCardById(map);

        System.out.println("map1==>" + map1);
        map.put("ownerName", map1.get("ownerName"));
        map.put("license_Plate_Number", map1.get("license_Plate_Number"));
        map.put("user_id", map1.get("user_Id"));
        if (map.get("coupon_id") != null && map.get("coupon_id").toString().trim().equals("")) {
            map.put("coupon_id", null);
        }

        //添加到申请表中
        int i = orderMapper.addToBookingApplication(map);

        System.out.println("1==================");
        System.out.println(map);

        //添加司机业务表
        int j = orderMapper.addToDriverBusiness(map);

        System.out.println("2==================");
        System.out.println(map);


        //将申请表和司机业务表绑定到订单表中；

        int k = orderMapper.addToBusinessOrder(map);

        System.out.println("3==================");
        System.out.println(map);

        map.put("state", 3);

        //添加到订单状态中
        int x = orderMapper.addOrderState(map);

        System.out.println("4==================");
        System.out.println(map);

        if (i == 1 && j == 1 && k == 1 && x == 1) {
            stringResultJson.setSuccess("添加订单成功");
        } else {
            stringResultJson.setSuccess("添加订单失败");
        }

        return stringResultJson;
    }

    @Override
    public ResultJson<List<Map<String, Object>>> findAllUser() {
        List<Map<String, Object>> list = orderMapper.findAllUser();
        mapListResultJson.setSuccess("查询用户成功", list);
        return mapListResultJson;
    }

    @Override
    public ResultJson<List<Map<String, Object>>> findAllOwnerCardByUserId(Integer id) {
        List<Map<String, Object>> list = orderMapper.findAllOwnerCardByUserId(id);

        mapListResultJson.setSuccess("查询车主卡成功", list);
        return mapListResultJson;

    }

    @Override
    public ResultJson<List<Map<String, Object>>> findAllDriver() {
        List<Map<String, Object>> list = orderMapper.findAllDriver();

        mapListResultJson.setSuccess("查询司机成功", list);

        return mapListResultJson;
    }

    @Override
    public ResultJson<List<Map<String, Object>>> findCouponByUserId(Integer id) {

        List<Map<String, Object>> list = orderMapper.findCouponByUserId(id);
        mapListResultJson.setSuccess("查询该用户的优惠券成功", list);

        return mapListResultJson;
    }
}
